
from PyQt4.QtGui import *
from PyQt4.QtCore import SIGNAL, Qt
from S3D.ElectricCraft.UI.MainWindow import Ui_MainWindow
from S3D.ElectricCraft.Renderer import Renderer
from S3D.ElectricCraft.Editor import Editor

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.renderer   = Renderer()
        self.connect(self.renderer, SIGNAL("GridResized()"), self.onGridResized)

        self.editor     = Editor(self)

        self.ui.board.setScene(self.renderer.scene)
        self.ui.board.mousePressEvent   = self.board_mousePressEvent
        self.ui.board.mouseReleaseEvent = self.board_mouseReleaseEvent
        self.ui.board.mouseMoveEvent    = self.board_mouseMoveEvent
        self.ui.board.mouseDoubleClickEvent = self.board_mouseDoubleClickEvent
        #self.ui.board.keyPressEvent     = self.board_keyPressEvent

        self.connect(self.editor,       SIGNAL("ResetHighlight()"),         self.renderer.resetHighlight)
        self.connect(self.editor,       SIGNAL("SetHighlight(int, int)"),   self.renderer.setHighlight)

        self.startTimer(0)

        self.onGridResized()

    def onGridResized(self):
        self.ui.board.setSceneRect(self.renderer.scene.sceneRect())

    def board_mouseMoveEvent(self, e):
        pos = self.ui.board.mapToScene(e.pos())
        pos = self.renderer.mapToGrid(pos)

        self.editor.onMouseMove(pos)

    def board_mousePressEvent(self, e):
        pos = self.ui.board.mapToScene(e.pos())
        pos = self.renderer.mapToGrid(pos)

        isControl = (e.modifiers() & Qt.ControlModifier)
        isShift   = (e.modifiers() & Qt.ShiftModifier)

        if e.button() == Qt.LeftButton:
            self.editor.onMouseLeftPress(pos, control = isControl, shift = isShift)
        elif e.button() == Qt.RightButton:
            self.editor.onMouseRightPress(pos, control = isControl, shift = isShift)

    def board_mouseReleaseEvent(self, e):
        pos = self.ui.board.mapToScene(e.pos())
        pos = self.renderer.mapToGrid(pos)

        if e.button() == Qt.LeftButton:
            self.editor.onMouseLeftRelease(pos)
        elif e.button() == Qt.RightButton:
            self.editor.onMouseRightRelease(pos)

    def board_mouseDoubleClickEvent(self, e):
        pos = self.ui.board.mapToScene(e.pos())
        pos = self.renderer.mapToGrid(pos)

        isControl = (e.modifiers() & Qt.ControlModifier)
        isShift   = (e.modifiers() & Qt.ShiftModifier)

        if e.button() == Qt.LeftButton:
            self.editor.onMouseDoubleClick(pos, control = isControl, shift = isShift)

    def getKeyName(self, key):
        if key >= Qt.Key_0 and key <= Qt.Key_9:
            return "0123456789"[key - Qt.Key_0]
        if key >= Qt.Key_A and key <= Qt.Key_Z:
            return "abcdefghigklmnopqrstuvwxyz"[key - Qt.Key_A]
        if key == Qt.Key_Space:
             return ' '
        if key == Qt.Key_Escape:
            return 'esc'

        return None

    def keyPressEvent(self, e):
        if e.isAutoRepeat():
            return

        keyName = self.getKeyName(e.key())
        if keyName is not None:
            isControl = (e.modifiers() & Qt.ControlModifier)
            isShift   = (e.modifiers() & Qt.ShiftModifier)

            self.editor.onKeyPress(keyName, control = isControl, shift = isShift)

    def keyReleaseEvent(self, e):
        if e.isAutoRepeat():
            return

        keyName = self.getKeyName(e.key())
        if keyName is not None:
            self.editor.onKeyRelease(keyName)

    def timerEvent(self, e):
        self.editor.update()